package com.Leetcode;

import java.util.HashMap;
import java.util.Map;

public class Index1410entityParser {

	public static void main(String[] args) {
		String a = "leetcode.com&frasl;problemset&frasl;all";
		a = entityParser(a);
		System.out.println(a);
	}
	
	public static String entityParser(String text) {
		Map<String,String> map = new HashMap<>();
		int left, right, len = text.length();
		map.put("&quot;", "\"");
		map.put("&apos;", "'");
		map.put("&amp;", "&");
		map.put("&gt;", ">");
		map.put("&lt;", "<");
		map.put("&frasl;", "/");
		
		for(left = right = len - 1;right >= 0;right = --left) {
			if(text.charAt(right) == ';') {
				left = right - 1;
				while(text.charAt(left) != '&') {
					if(text.charAt(left) == ';') right = left;
					if(left == 0) return text;
					left --;
				}
				String str = text.substring(left,right + 1);
				if(map.containsKey(str)) 
					text = text.substring(0,left) +  map.get(str) + text.substring(right + 1);
				}
		}
		return text;
    }
}
